#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int* getOffsets(char *string, char lookup) {
	int i = -1;
    int *result = NULL;
	int resultSize = 0;
    
    while (string[++i] != '\0') {
		if (string[i] == lookup) {
			resultSize++;
		}
	}
    
    
    result = malloc(resultSize*sizeof(int));   

    i=-1;    
    resultSize=0;
    
	printf("string: %s\n", string);
	printf("string (char by char):\n\n");
	while (string[++i] != '\0') {
		printf("#%i: %c\n", i, string[i]);
		if (string[i] == lookup) {
			result[resultSize] = i;
            resultSize++;
		}
	}
	return result;
}

int main(void) {
	char *dirname, *year, *month;
	int* pubDatePartsOffsets;
	char* pubDate = "Wed, 15 Dec 2010 12:00:00 +0100";
	// make a copy of the pubDate, split it into loose strings 
	//pubDateParts = malloc((strlen(pubDate) + 1)*sizeof(char));
	//strncpy(pubDateParts, pubDate, (strlen(pubDate) + 1));
	pubDatePartsOffsets = getOffsets(pubDate, ' ');
    
    int i;
    for(i=0; i<(sizeof(pubDatePartsOffsets)/sizeof(int*)); i++){
        printf("OFFSET %d: %d\n", i, pubDatePartsOffsets[i]);
    }
	// form the directory name from it in which the file should be placed
	// assuming pubDate was formatted like Wed, 15 Dec 2010 12:00:00 +0100
	// the format of dirname will be 2010-Dec, so:
	dirname = malloc(8*sizeof(char));
    year = malloc(4*sizeof(char));
    month = malloc(3*sizeof(char));
    strncpy(year, 1+pubDate+pubDatePartsOffsets[2], 4);
    strncpy(month, 1+pubDate+pubDatePartsOffsets[1], 3);
	sprintf(dirname, "%s-%s", year, month);
    printf("month:%s, year:%s, dirname:%s", month, year, dirname);
	// strncpy(dirname, &pubDateParts[pubDatePartsOffsets[2]+1], 4); // +1 because: see replaceChars return value
	// dirname[4] = '-';
	// strncpy(&dirname[5], &pubDateParts[pubDatePartsOffsets[1]+1], 3);
    
    return 0;
}

